package com.uchiha.nova.core.dynamic;

import com.baomidou.dynamic.datasource.aop.DynamicDataSourceAnnotationInterceptor;
import com.baomidou.dynamic.datasource.processor.DsProcessor;
import com.uchiha.nova.core.tool.util.AuthUtil;
import lombok.Setter;
import org.aopalliance.intercept.MethodInvocation;

/**
 * @author 周林静
 * @Date 2023-01-09,0009 14:35
 * @Version 1.0
 */
public class TenantDataSourceAnnotationInterceptor extends DynamicDataSourceAnnotationInterceptor {

    @Setter
    private TenantDataSourceHolder holder;

    public TenantDataSourceAnnotationInterceptor(Boolean allowedPublicOnly, DsProcessor dsProcessor) {
        super(allowedPublicOnly, dsProcessor);
    }

    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        try {
            holder.handleDataSource(AuthUtil.getUser().getTenantId());
            return super.invoke(invocation);
        } catch (Exception exception) {
            throw new TenantDataSourceException(exception.getMessage());
        }
    }

}

